package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private Teacher teacher = new Teacher();
    private TeacherService teacherService = new TeacherService();

    //老师管理界面展示
    public void start() {
        //菜单循环展示
        while (true) {
            System.out.println("-----欢迎使用老师管理-----");
            System.out.println("请输入您的选择:1.添加老师2.删除老师3.修改老师4.查看老师5.退出");
            //键入选择
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacherById();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用!");
                    return;
                default:
                    System.out.println("输入有误,请重新输入!");
                    break;
            }
        }
    }

    //建立修改老师方法
    private void updateTeacher() {
        //判断数组是否为空,为空则不进行操作
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("暂无数据,请先录入");
            return;
        }
        //录入老师ID
        String updateId = inputTeacherId(false);
        //键入老师信息
        Teacher teacher = inputTeacherInfo(updateId);
        //调用service中修改老师方法
        teacherService.updateTeacher(teacher);
        System.out.println("修改成功!");
    }

    //建立查看所有老师方法
    public void findAllTeacher() {
        //判断数组是否为空,为空则不进行操作
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("暂无数据,请先录入");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t\t生日");
        //遍历循环输出
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null) {
                teacher = teachers[i];
                System.out.println(teacher.getId() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "\t\t" + teacher.getBirthday());
            }
        }
    }

    //建立删除老师方法
    public void deleteTeacherById() {
        //判断数组是否为空,为空则不进行操作
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null) {
            System.out.println("暂无数据,请先录入");
            return;
        }
        //调用录入工号方法
        String delId = inputTeacherId(false);
        //调用service中删除方法
        teacherService.deleteTeacherById(delId);
        System.out.println("删除成功!");
    }

    //添加老师方法
    public void addTeacher() {
        //引导键入工号并进行判断
        String id = inputTeacherId(true);
        //键入老师信息并封装
        teacher = inputTeacherInfo(id);
        //调用service添加老师方法并判断是否添加成功(满/未满)
        boolean flag = teacherService.addTeacher(teacher);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //键入老师信息方法
    //开闭原则:对扩展内容进行开放,对修改内容进行关闭
    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        String age = sc.next();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        //封装老师信息
        Teacher th = new Teacher();
        th.setId(id);
        th.setName(name);
        th.setAge(age);
        th.setBirthday(birthday);
        return th;
    }

    //键入工号方法
    private String inputTeacherId(boolean flag) {
        String id;
        //建立死循环直到输入正确值跳出
        while (true) {
            System.out.println("请输入工号:");
            id = sc.next();
            //判断工号是否已经存在
            boolean exists = teacherService.isExists(id);
            if (exists == flag) {
                //已存在
                if (flag == true) {
                    System.out.println("用户已存在,请重新输入!");
                } else {
                    System.out.println("用户不存在,请重新驶入!");
                }
            } else {
                break;
            }
        }
        return id;
    }
}
